/*********************************************************************************
 *      Copyright:  (C) 2025 LingYun<iot25@lingyun>
 *                  All rights reserved.
 *
 *       Filename:  fork-var.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(09/03/25)
 *         Author:  LingYun <iot25@lingyun>
 *      ChangeLog:  1, Release initial version on "09/03/25 19:21:36"
 *                 
 ********************************************************************************/
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
int       g_var = 6;
char      g_buf[]=" A string write to stdout.\n";
int main(int argc,char *argv[])
{
	int          var=88;
	pid_t        pid;
	if( write(STDOUT_FILENO,g_buf,sizeof(g_buf))<0)
	{
		printf("Write string to stdout error: %s\n",strerror(errno));
		return -1;
	}
	printf("Berof fork\n");

	if( ( pid=fork() )<0 )
	{
		printf(" fork() error: %s\n",strerror(errno));
		return -2;
	}
	else if( pid==0 )
	{
		printf("Child process PID[%d] running...\n",getpid());
		g_var ++;
		var ++;
	}
	else 
	{
		printf("Parent process PID[%d] waiting...\n",getpid());
		sleep(1);
	}
	printf("PID=%ld,g_var=%d, var=%d\n",(long) getpid(),g_var,var);
	return 0;
}

